//
//  TextViewCell.m
//  KuWoCan
//
//  Created by hwd on 17/7/25.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "TextViewCell.h"

@implementation TextViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
    }
    return self;
}

- (void)setUpUIWithTextStr:(NSString *)textStr;
{
    
    _textView = [[CanTextView alloc]initWithFrame:CGRectMake(0, 0, SCREENW-50*screenW_6, 0)];
    _textView.delegate = self;
    [_textView configTextView];

    [self addSubview:_textView];
    _textView.text = textStr;
    float  Textheight = [self heightForTextView:_textView WithText:_textView.text];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-25*screenW_6));
        make.left.equalTo(@(25*screenW_6));
        make.top.equalTo(self.mas_top).offset(15*screenW_6);
        make.height.equalTo(@(Textheight));
        make.bottom.equalTo(self.mas_bottom).offset(-20*screenW_6);
    }];
    NSLog(@"height=======%f",Textheight);
    _textHeight = Textheight;
    __weak typeof(self)weakSelf = self ;
    _textView.heightblock = ^(float height){
        
//        if (height > _textHeight + 20 || height < _textHeight - 20) {
//            if (weakSelf.reloadTextBlock) {
//                weakSelf.reloadTextBlock(weakSelf.textView);
//            }
//            _textHeight = height;
//        }
    
        [weakSelf.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-25*screenW_6));
            make.left.equalTo(@(25*screenW_6));
            make.top.equalTo(weakSelf.mas_top).offset(10*screenW_6);
            make.height.equalTo(@(height));
            make.bottom.equalTo(weakSelf.mas_bottom).offset(-20*screenW_6);
        }];
    };
}

- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                        context:nil];
    float textHeight = size.size.height + 30.0;
    
    return textHeight;
}
-(void)textViewDidChange:(UITextView *)textView{
    if (_reloadTextBlock)
    {
        self.reloadTextBlock(textView);
//        if ([textView.text isEqual:@""] || textView.text.length<=0) {
//            self.reloadTextBlock(@"");
//        }else
//        {
//            self.reloadTextBlock(textView.text);
//        }
        
    }
}
@end
